package net.zjitc.day09;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 2017/9/27.
 */
@WebServlet(name = "CountServlet",urlPatterns = "/countServlet")
public class CountServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 统计访问，放在哪里？？
        ServletContext context = this.getServletContext();
        response.setContentType("text/html;charset=utf-8");
        //context里面设置一个属性 count ---》n
        PrintWriter out = response.getWriter();

        Integer count;
        count = (Integer) context.getAttribute("count");  //null

        if (count == null){
            count= 1;
            //第一次访问，页面
            out.write("这是你第+"+count+"+次访问<br/>");
        }else {
            count++;
            out.write("这是你第"+count+"次访问<br/>");
        }
        out.write("hahahhhahahhaa");
        //保存到context,下次才能取到+1的值
        context.setAttribute("count",count);

    }
}
